﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace PHRClient
{
    public partial class MainForm : Form
    {
        public static LoginForm _loginForm = null;
        public static PHRDetails _phrDetails = null;

        private static TcpListener tcpListener;
        private static Thread listenThread;

        public MainForm()
        {
            InitializeComponent();            
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            _loginForm = new LoginForm();
            _loginForm.Show();            
        }

        public static void PortListnerStart()
        {
            if (listenThread == null)
            {
                tcpListener = new TcpListener(IPAddress.Any, 3000);
                listenThread = new Thread(new ThreadStart(ListenForClients));
                listenThread.IsBackground = true; 
                listenThread.Start();
            }
        }

        public static void PortListnerStop()
        {
            //listenThread.Abort();
        }

        private static void ListenForClients()
        {
            tcpListener.Start();

            while (Program.isAppRunning)
            {
                //blocks until a client has connected to the server
                TcpClient client = tcpListener.AcceptTcpClient();

                //create a thread to handle communication 
                //with connected client
                Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
                clientThread.IsBackground = true; 
                clientThread.Start(client);
            }
        }

        private static void HandleClientComm(object client)
        {
            TcpClient tcpClient = (TcpClient)client;
            NetworkStream clientStream = tcpClient.GetStream();

            byte[] message = new byte[4096];
            int bytesRead;

            while (Program.isAppRunning)
            {
                bytesRead = 0;

                try
                {
                    //blocks until a client sends a message
                    bytesRead = clientStream.Read(message, 0, 4096);
                }
                catch
                {
                    //a socket error has occured
                    break;
                }

                if (bytesRead == 0)
                {
                    //the client has disconnected from the server
                    break;
                }

                //message has successfully been received
                ASCIIEncoding encoder = new ASCIIEncoding();
                System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));
                // MessageBox.Show(message.ToString());
                String messageIncoming = encoder.GetString(message, 0, bytesRead);
                // MessageBox.Show(messageIncoming);
                string temp = messageIncoming.ToString();// EnCryptDecrypt.CryptorEngine.Decrypt(messageIncoming, true);
               // string[] wordsIncoming = temp.Split('-');
                // MessageBox.Show(wordsIncoming[0]);
                //string userId = wordsIncoming[0];
               // string userAuthentication = wordsIncoming[1];
               // MessageBox.Show(temp);
                if (temp != "")
                {
                    Validator.loginValidate(temp);
                    break;
                }

            }

            tcpClient.Close();
        }
    }
}
